package com.jmebananas.game.hud;

import javacream.scene.sprite.Sprite;
import javacream.scene.sprite.SpriteAnimation;

import com.jmebananas.game.tile.SpriteTile;

/**
 * SpriteButton
 *
 * @author Glenn Powell
 *
 */
public class SpriteButton extends SpriteTile {

	private static final long serialVersionUID = 1L;
	
	
	private String idleAnimation = null;
	
	private String hoverAnimation = null;
	
	private String pressAnimation = null;
	

	public SpriteButton(String name) {
		this(name, null, true);
	}
	
	public SpriteButton(String name, boolean animates) {
		this(name, null, animates);
	}

	public SpriteButton(String name, Sprite sprite) {
		this(name, sprite, true);
	}

	public SpriteButton(String name, Sprite sprite, boolean animates) {
		super(name, sprite, animates);
	}
	
	public SpriteAnimation getIdleAnimation() {
		if (idleAnimation != null)
			return getSprite().findAnimation(idleAnimation);
		return getSprite().getAnimation(0);
	}
	
	public void setIdleAnimation(String animation) {
		idleAnimation = animation;
	}
	
	public SpriteAnimation getHoverAnimation() {
		if (hoverAnimation != null)
			return getSprite().findAnimation(hoverAnimation);
		return getSprite().getAnimation(1);
	}

	public void setHoverAnimation(String animation) {
		hoverAnimation = animation;
	}

	public SpriteAnimation getPressAnimation() {
		if (pressAnimation != null)
			return getSprite().findAnimation(pressAnimation);
		return getSprite().getAnimation(2);
	}
	
	public void setPressAnimation(String animation) {
		pressAnimation = animation;
	}

	public void onButton(int button, boolean pressed, int x, int y) {
		getSprite().setCurrentAnimation(pressed ? getPressAnimation()
				: ((x >= 0 && x < getWidth() && y >= 0 && y < getHeight())
				? getHoverAnimation() : getIdleAnimation()));
	}

	public void onEnter(int x, int y) {
		getSprite().setCurrentAnimation(getHoverAnimation());
	}

	public void onExit() {
		getSprite().setCurrentAnimation(getIdleAnimation());
	}

}
